<?php
  error_reporting(0);
  session_start();
  if ($_SESSION["tipo"]!=1){
    header ("Location: index.php");
  }
  include_once("seguridad.php");
  include_once("fachadaBD.php");
  include("top.php");
  include("left.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Compras Revisadas</title>
    </head>
    <body>
      <div id="contenido3">
        <div id="especificacion">
        <!table class="AdminTableCompra">
          <?php
            $f = new fachadaBD();
            function tabla($entrada,$tipo){
              $cont = 1;
              echo "<div class=\"AdminCompra2\">";
              echo "<table class=\"AdminTableCompra3\">";
              while ($row = mysql_fetch_assoc($entrada)){
                $query = "SELECT * FROM USUARIO WHERE CORREO ='".$row["USUARIO"]."'";
                $user = mysql_fetch_assoc(mysql_query($query));
                if (!$user)
                  echo "<tr><td>aqui ".mysql_errno()." </td></tr>";
                if ($cont % 2 == 0){
                  echo "<tr class=\"AdminContenidoCompra1\"> <td style=\"width: 75px;\">";
                }else{
                  echo "<tr class=\"AdminContenidoCompra0\"> <td style=\"width: 75px;\">";
                }
                //$user = mysql_fetch_assoc($f->getUserByEmail($row["USUARIO"]));
                                echo $row["PRODUCTO"];
                echo "</td> <td style=\"width: 75px;\">";
                echo $row["FECHA"];
                //echo "</td> <td style=\"width: 120px;\">";
                echo "</td> <td style=\"width: 70px;\">";
                echo $row["USUARIO"];
                echo "</td> <td style=\"width: 25px;\">";
                echo $row["CANTIDAD"];
                echo "</td> <td>";
                if ($row["TIPO_PAGO"] == 0){
                  echo "Deposito";
                }else if ($row["TIPO_PAGO"] == 1){
                  echo "Transferencia Electronica";
                }else if ($row["TIPO_PAGO"] == 2){
                  echo "Tarjeta de Credito";
                }
                echo "</td> <td>";
                if ($row["TIPO_ENTREGA"] == 0){
                  echo "MRW";
                }else if($row["TIPO_ENTREGA"] == 1){
                  echo "Grupo Zoom";
                }else if($row["TIPO_ENTREGA"] == 2){
                  echo "Retiro personal";
                }
                echo "</td> <td>";
                /*
                echo "<form name=\"fr".$cont."\" method=\"POST\" action =\"reprobar.php\" >";
                echo "<input name=\"id\" type=\"hidden\" value =".$row["ID"].">";
                echo "</form>";
                echo "<form name=\"fa".$cont."\" method=\"POST\" action =\"aprobar.php\" >";
                echo "<input name=\"id\" type=\"hidden\" value =".$row["ID"].">";
                echo "</form>";
                echo "<button onclick=\"verif('fa".$cont."')\">si";
                echo "</td> <td>";
                echo "<button onclick=\"verif('fr".$cont."')\">no";
                */
                $param = $tipo*$cont;
                echo "<button id=\"b".$param."\" value=\"0\" onclick=\"show(".$param.")\">Datos Comprador</button> </td>";
                if ($tipo == 7){
                  echo "<form action=\"concretada.php\" method=\"POST\" name=\"c".$param."\">";
                  echo "<input type=\"hidden\" name =\"id\" value=\"$row[ID]\">";
                  echo "</form>";
                  echo "<td><button id=\"c".$param."\" value=\"0\" onclick=\"verifPago('c".$param."')\">Concretada?</button></td>";
                  echo "<td><button id=\"a".$param."\" value=\"0\" onclick=\"anular('a".$param."')\">anularOrden?</button></td>";
                  echo "<form action=\"anular.php\" method=\"POST\" name=\"a".$param."\">";
                  echo "<input type=\"hidden\" name =\"id\" value=\"'$row[ID]'\">";
                  echo "</form>";
                }else if ($tipo == 11){
                  if (($row["TIPO_ENTREGA"] == 0 || $row["TIPO_ENTREGA"] == 1) && $row["TRACKING"] == ""){
                    echo "<td><button id=\"c".$param."\" onclick=\"showTracking(".$param.")\">Tracking</button>";
                    echo "<form action=\"tracking.php\" method=\"POST\" name=\"c".$param."\">";
                    echo "<input type=\"hidden\" name=\"id\" value=\"$row[ID]\">";
                    echo "<input type=\"text\" name=\"tracking\">";
										echo "<p>Costo del envio:</p>";
                    echo "<input type=\"text\" name=\"precio\">";
                    echo "<p>Fecha de Entrega</p>";
                    echo "<input type=\"text\" name=\"fecha\">";
                    echo "</form></td>";
                  }
                }
                echo " </tr>";
                if ($cont % 2 == 0){
                  echo "<tr id=\"".$param."\" style=\"visibility: hidden;\" class=\"AdminContenidoCompra0\">";
                }else{
                  echo "<tr id=\"".$param."\" class=\"AdminContenidoCompra1\" style=\"visibility: hidden;\">";
                }
                echo "<td> ".$user["NOMBRE"]." </td>";
                echo "<td> ".$user["APELLIDO"]."</td>";
                echo "<td> ".$user["CIPHER"]."</td>";
                echo "<td> ".$user["TELEFONO"]."</td>";
                echo "<td></td><td></td><td></td><td></td>";
                echo "</tr>";
                ++$cont;
              }
              echo "</table>";
              echo "</div>";
            }
            ?>
        <h3>Compras Canceladas</h3>
        <?php
            $ret = $f->getComprasRevisadas(0);
            tabla($ret,3);
           ?>
        <h3>Compras Aprobadas</h3>
        <?php
            $ret = $f->getComprasRevisadas(1);
            tabla($ret,7);
            // Comprobar resultado
            // El siguiente código muestra la consulta enviada a MySQL y el error ocurrido.
            //  Útil para debugging.
      //    if (!$ret) {
      //      $message = 'Invalid query: ' . mysql_error() . "\n";
      //      $message .= 'Whole query: ' . $query;
      //      die($message);
      //    }
          ?>
        <h3>Compras Concretadas</h3>
        <?php
          $ret =$f->getComprasConcretadas();
          tabla($ret,11);
        ?>
        <script language="JavaScript" type="text/javascript">
          function show(num){
            if (document.getElementById("b"+num).getAttribute("value") == 0){
              document.getElementById(num).setAttribute('style','visibility: visible');
              document.getElementById("b"+num).value = 1;
            }else{
              document.getElementById(num).setAttribute('style','visibility: hidden');
              document.getElementById("b"+num).value = 0;
            }
          }
          function verif(num){
            var reprobar=/fr\d*/;
            var aprobar=/fa\d*/;
            if (num.match(reprobar)){
              var answer = confirm("Desea reprobar la compra?");
            }else if(num.match(aprobar)){
              var answer = confirm("Desea aprobar la compra?");
            }
            if (answer){
              document.forms[num].submit();
            }
          }

          function verifPago(num){
            var answer = confirm("Desea marcar esta compra como concretada?");
            if (answer)
              document.forms[num].submit();
          }

          function showTracking(num){
            var answer = confirm("Seguro que coloco el tracking a esta compra, su respectivo costo del envio y fecha de entrega?");
            if (answer)
              document.forms["c"+num].submit();
          }

          function anular(num){
            var answer = confirm("Seguro que desea anular esta compra?");
            if (answer)
              document.forms[num].submit();
          }

        </script>
      <!table>
      </div>
      </div>
<?php
  include("bottom.php");
?>
